﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mesoBoard.Web.Core;
using mesoBoard.Data;

namespace mesoBoard.Web.Areas.Admin.Controllers
{
    public class ForumsController : mesoBoardAdminController
    {
        public ActionResult Forums()
        {
            SetCrumb("Forums");
            return View(Repositories.Categories.GetCategories());
        }

        public ActionResult DeleteForum(int ForumID)
        {
            Repositories.Forums.DeleteForum(ForumID);
            SetSuccess("Forum Deleted");
            return RedirectToAction("Forums");
        }

        public ActionResult DeleteCategory(int CategoryID)
        {
            Repositories.Categories.DeleteCategory(Repositories.Categories.GetCategory(CategoryID));
            SetSuccess("Category Deleted");
            return RedirectToAction("Forums");
        }

        [HttpGet]
        public ActionResult CreateCategory()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateCategory(Category TheCat)
        {
            SetCrumb("Forums");
            if (string.IsNullOrEmpty(TheCat.Name))
                ModelState.AddModelError("Name", "Enter a category name");

            if (ModelState.IsValid)
            {
                Repositories.Categories.CreateCategory(TheCat);
                return RedirectToAction("Forums");
            }
            else
            {
                return View(TheCat);
            }
        }

        [HttpGet]
        public ActionResult CreateForum()
        {
            SetCrumb("Forums");
            ViewData["CategoryID"] = new SelectList(Repositories.Categories.GetCategories(), "CategoryID", "Name");
            return View();
        }

        [HttpPost]
        public ActionResult CreateForum(Forum TheForum)
        {
            SetCrumb("Forums");
            if (string.IsNullOrEmpty(TheForum.Name))
                ModelState.AddModelError("Name", "Enter a forum name");

            if (ModelState.IsValid)
            {
                Repositories.Forums.CreateForum(TheForum);
                SetSuccess("Forum created");
                return RedirectToAction("Forums");
            }
            else
            {
                ViewData["CategoryID"] = new SelectList(Repositories.Categories.GetCategories(), "CategoryID", "Name");
                return View(TheForum);
            }
        }

        [HttpGet]
        public ActionResult EditForum(int ForumID)
        {
            SetCrumb("Forums");
            Forum TheForum = Repositories.Forums.GetForum(ForumID);
            ViewData["CategoryID"] = new SelectList(Repositories.Categories.GetCategories(), "CategoryID", "Name", TheForum.CategoryID);
            return View(TheForum);
        }

        [HttpPost]
        public ActionResult EditForum(Forum TheForum)
        {
            SetCrumb("Forums");
            if (string.IsNullOrEmpty(TheForum.Name))
                ModelState.AddModelError("Name", "Enter a forum name");

            if (ModelState.IsValid)
            {
                Repositories.Forums.UpdateForum(TheForum);
                Repositories.Forums.SyncForum(TheForum.ForumID);
                SetSuccess("Forum Updated");
                return RedirectToAction("Forums");
            }
            else
            {
                ViewData["CategoryID"] = new SelectList(Repositories.Categories.GetCategories(), "CategoryID", "Name", TheForum.CategoryID);
                return View(TheForum);
            }
        }


        [HttpGet]
        public ActionResult EditCategory(int CategoryID)
        {
            SetCrumb("Forums");
            return View(Repositories.Categories.GetCategory(CategoryID));
        }

        [HttpPost]
        public ActionResult EditCategory(Category TheCategory)
        {
            SetCrumb("Forums");
            if (string.IsNullOrEmpty(TheCategory.Name))
                ModelState.AddModelError("Name", "Enter a category name");

            if (ModelState.IsValid)
            {
                Repositories.Categories.UpdateCategory(TheCategory);
                SetSuccess("Category Updated");
                return RedirectToAction("Forums");
            }
            else
            {
                return View(TheCategory);
            }
        }

        public ActionResult Move(int Direction, int? ForumID, int CategoryID)
        {
            if (ForumID.HasValue)
                mbServices.Forums.ChangeForumOrder(ForumID.Value, CategoryID, Direction);
            else
                mbServices.Forums.ChangeCategoryOrder(CategoryID, Direction);
            
            SetSuccess("Order Changed");
            return RedirectToAction("Forums");
        }

        public ActionResult ForumPermissions(int ForumID)
        {
            SetCrumb("Forums");
            ViewData["TheForum"] = Repositories.Forums.GetForum(ForumID);
            return View(Repositories.Forums.GetPermissions(ForumID));
        }

        [HttpGet]
        public ActionResult CreateForumPermission(int? ForumID)
        {
            SetCrumb("Forums");
            ViewData["RoleID"] = new SelectList(Repositories.Roles.GetAllRoles(), "RoleID", "Name");
            ViewData["ForumID"] = new SelectList(Repositories.Forums.GetForums(), "ForumID", "Name");
            return View();
        }

        [HttpPost]
        public ActionResult CreateForumPermission(ForumPermission fPerm)
        {
            SetCrumb("Forums");
            ViewData["RoleID"] = new SelectList(Repositories.Roles.GetAllRoles(), "RoleID", "Name");
            ViewData["ForumID"] = new SelectList(Repositories.Forums.GetForums(), "ForumID", "Name");

            if (Repositories.Permissions.PermissionExists(fPerm.ForumID, fPerm.RoleID))
            {
                SetError("Permission for the role/forum combination already exists");
                return View(fPerm);
            }
            else
            {
                SetSuccess("Permission added");
                fPerm.Name = "Placeholder";
                Repositories.Permissions.CreateForumPermission(fPerm);
                return RedirectToAction("ForumPermissions", new { ForumID = fPerm.ForumID });
            }
        }

        public ActionResult DeleteForumPermission(int PermissionID)
        {
            ForumPermission fPerm = Repositories.Permissions.GetForumPermission(PermissionID);
            Repositories.Permissions.DeleteForumPermission(fPerm);
            SetSuccess("Forum permission deleted");
            return RedirectToAction("ForumPermissions", new { ForumID = fPerm.ForumID });
        }

        [HttpGet]
        public ActionResult EditForumPermission(int PermissionID)
        {
            SetCrumb("Forums");
            ForumPermission fPerm = Repositories.Permissions.GetForumPermission(PermissionID);
            return View(fPerm);
        }

        [HttpPost]
        public ActionResult EditForumPermission(ForumPermission fPerm)
        {
            SetCrumb("Forums");
            ForumPermission originalPerms = Repositories.Permissions.GetForumPermission(fPerm.ForumPermissionID);

            fPerm.EntityKey = originalPerms.EntityKey;

            Repositories.Permissions.UpdatePermission(fPerm);
            SetSuccess("Forum permissions updated");
            return View(Repositories.Permissions.GetForumPermission(fPerm.ForumPermissionID));
        }

    }
}
